#include "derivative.h" /* include peripheral declarations */
#include "i2c.h"
#include "i2c_interface.h"
#include "uart.h"
#include "evhdl_iterface.h"
#include "MPR121.h"
#include "MMA8451.h"

/***********************************************************************************************\
* Private macros
\***********************************************************************************************/

/***********************************************************************************************\
* Private prototypes
\***********************************************************************************************/

/***********************************************************************************************\
* Private memory declarations
\***********************************************************************************************/

/***********************************************************************************************\
* Public memory declarations
\***********************************************************************************************/
extern I2C__tstDevData I2C__Device[I2C_enNumOfDevices];

uint8 I2C__DeviceModuel[I2C_enNumOfDevices] ={
		0x00, /*I2C_enTouchDev*/
		0x00, /*I2C_enAccelDev*/
		0x01, /*I2C_enMemDev*/		
};

uint8 I2C__DeviceAddr[I2C_enNumOfDevices] ={
		0x5A, /*I2C_enTouchDev*/
		0x1D, /*I2C_enAccelDev*/
		0x01, /*I2C_enMemDev*/		
};


const pFuncParam_t I2C__apfAppEventFunc[I2C_enNumOfDevices] = {
      MPR__vFinishjobHandler,
      MMA84x__vFinishjobHandler,
      NULL,
};

/***********************************************************************************************\
* Public functions
\***********************************************************************************************/

void I2C_vSini(void)
{
	uint8 x = 0;
	
    for (x = 0; x < I2C_enNumOfDevices; x++)
	{
    	I2C__Device[x].u8i2cModule = I2C__DeviceModuel[x];
    	I2C__Device[x].u8SlaveAddr = I2C__DeviceAddr[x];
    	I2C__Device[x].DeviceEvent = I2C__apfAppEventFunc[x];
	}
    
	EVHD_vRegister(EV_I2C_Chann0_TxFinish_Event);
	EVHD_vRegister(EV_I2C_Chann1_TxFinish_Event);
	EVHD_vRegister(EV_I2C_Chann0_RxFinish_Event);
	EVHD_vRegister(EV_I2C_Chann1_RxFinish_Event);	
}


void I2C_vInitHW(uint8 u8channel, uint8 u8Addr, uint32 u32bps)
{
	SIM_SCGC4 |= 0x40;  /* Enable i2c_0 clock gate for KL25*/
	i2c_init(u8channel, u8Addr, u32bps);
}

void I2C_ReadJob(I2C_tenDevices tenDevice, uint8 u8StartAddr, uint8 u8nBytes, uint8 *u8buffPtr)
{
	I2C__Device[tenDevice].u8Tx_index = 0;
	I2C__Device[tenDevice].u8Txlength = 1;
	I2C__Device[tenDevice].u8Rx_index = 0;
	I2C__Device[tenDevice].u8Rxlength = u8nBytes;
	I2C__Device[tenDevice].u8RxCopylength = u8nBytes;
	I2C__Device[tenDevice].u8TxBuff[0] = u8StartAddr;
	I2C__Device[tenDevice].bActive = TRUE;
	I2C__Device[tenDevice].u8pAppBuffPtr = u8buffPtr;

	I2C_StartJob(I2C__Device[tenDevice].u8i2cModule, I2C_TXRX,
			I2C__Device[tenDevice].u8SlaveAddr);
}

void I2C_WriteJob(I2C_tenDevices tenDevice, uint8 u8StartAddr, uint8 *u8Buffptr, uint8 u8nBytes)
{
   uint8 x = 0;

   I2C__Device[tenDevice].u8Tx_index = 0;
   I2C__Device[tenDevice].u8Txlength = u8nBytes + 1;
   I2C__Device[tenDevice].u8Rx_index = 0;
   I2C__Device[tenDevice].u8Rxlength = 0;
   I2C__Device[tenDevice].u8RxCopylength = 0;
   I2C__Device[tenDevice].u8TxBuff[0] = u8StartAddr;
   I2C__Device[tenDevice].bActive = TRUE;
   I2C__Device[tenDevice].u8pAppBuffPtr = NULL;

   for (x = 1; x < u8nBytes+1; x++)
   {
      I2C__Device[tenDevice].u8TxBuff[x] = u8Buffptr[x-1];
   }

   I2C_StartJob(I2C__Device[tenDevice].u8i2cModule, I2C_TX, I2C__Device[tenDevice].u8SlaveAddr);

}

void I2C_Chann0TxFinishHandler(void)
{
	I2C__TxFinish(I2C__nChannel0);
}

void I2C_Chann1TxFinishHandler(void)
{
	I2C__TxFinish(I2C__nChannel1);
}

void I2C_Chann0RxFinishHandler(void)
{
   I2C__RxFinish(I2C__nChannel0);
}

void I2C_Chann1RxFinishHandler(void)
{
   I2C__RxFinish(I2C__nChannel1);
}

bool I2C_bDeviceBusy(I2C_tenDevices tenDevice)
{
   return I2C__Device[tenDevice].bActive;
}
